package net.sxnic.ugr.user;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.sxinfo.core.entity.AbstractEntity;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.group.Group;
import net.sxnic.ugr.role.Role;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.IndexColumn;
import org.springframework.core.annotation.Order;

/**
 * 单位、用户实体
 * @author 孙宇飞 20131121
 * @version 申报平台v3.0
 */
@Entity
@Table(name = "ugr_user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends AbstractEntity {

	/**
	 * Serial
	 */
	private static final long serialVersionUID = 5006587923583141491L;

	/**
	 * 男性代码
	 */
	public static final int MALE = 1;

	/**
	 * 女性代码
	 */
	public static final int FEMALE = 2;

	/**
	 * 用户名，单位名 unique
	 */
	@IndexColumn(name = "username_index")
	@Column(unique = true, nullable = false)
	private String username;
	
	/**
	 * 联系人
	 */
	private String linkmen;

	/**
	 * 职务
	 */
	private String duty;

	/**
	 * 全名
	 */
	private String fullname;

	/**
	 * 密码
	 */
	@Column(nullable = false)
	private String password;
	
	//明码
	private String plainPassword;
	
	/**
	 * 签字密码
	 */
	private String signPwd;

	/**
	 * E-Mail
	 */
	private String email;

	/**
	 * 备注
	 */
	private String memo;

	/**
	 * 固定电话
	 */
	private String phone;

	/**
	 * 手机
	 */
	private String mobile;
	
	/**
	 * 传真
	 */
	private String fax;

	/**
	 * 身份证
	 */
	private String idcard;

	
	/**
	 * 最后登录日期
	 */
	private Date lastLoginDate;

	/**
	 * 最后一次登录IP
	 */
	private String lastLoginIp;

	/**
	 * 绑定ip
	 */
	private String boundIp;
	
	@Column(length=10)
	private String zzdwCate;		

	/**
	 * 用户排序
	 */
	@Order
	private int userOrder;		
	
	/**
	 * 所属子系统编码序列
	 */
	private String siteCodes;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Dept dept;
	
	
	@ManyToMany(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Group> groups;	
	
	/**
	 * Roles
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Role> roles;		

	/**
	 * 默认构造器
	 */
	public User() {
		super();
		roles = new HashSet<Role>();
		groups = new HashSet<Group>();
	}

	/**
	 * 构造器
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 */
	public User(String username, String password) {
		this();
		this.username = username;
		this.password = password;
		roles = new HashSet<Role>();
		groups = new HashSet<Group>();
		dept = new Dept();
	}

	/**
	 * 返回用户名
	 * 
	 * @return 用户名
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * 设置新用户名
	 * 
	 * @param username
	 *            新用户名
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return Returns the fullname.
	 */
	public String getFullname() {
		return fullname;
	}

	/**
	 * @param fullname
	 *            The fullName to set.
	 */
	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	/**
	 * 获取密码
	 * 
	 * @return 密码
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * 设置新密码
	 * 
	 * @param password
	 *            要设置的新密码
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return Returns the email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            The email to set.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return Returns the enabled.
	 */
	public boolean getEnabled() {
		return enabled;
	}

	/**
	 * @param enabled
	 *            The enabled to set.
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * @return Returns the roles.
	 */
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles
	 *            The roles to set.
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof User)) {
			return false;
		}

		final User u = (User) o;

		return new EqualsBuilder().appendSuper(super.equals(u))
				.append(username, u.getUsername()).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(username).toHashCode();
	}

	/**
	 * @return Returns the memo.
	 */
	public String getMemo() {
		return memo;
	}

	/**
	 * @param memo
	 *            The memo to set.
	 */
	public void setMemo(String memo) {
		this.memo = memo;
	}

	/**
	 * @return the duty
	 */
	public String getDuty() {
		return duty;
	}

	/**
	 * @param duty
	 *            the duty to set
	 */
	public void setDuty(String duty) {
		this.duty = duty;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @param mobile
	 *            the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @return the idcard
	 */
	public String getIdcard() {
		return idcard;
	}

	/**
	 * @param idcard
	 *            the idcard to set
	 */
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	/**
	 * @return the lastLoginDate
	 */
	public Date getLastLoginDate() {
		return lastLoginDate;
	}

	/**
	 * @param lastLoginDate
	 *            the lastLoginDate to set
	 */
	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}

	/**
	 * @return the lastLoginIp
	 */
	public String getLastLoginIp() {
		return lastLoginIp;
	}

	/**
	 * @param lastLoginIp
	 *            the lastLoginIp to set
	 */
	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public int getUserOrder() {
		return userOrder;
	}

	public void setUserOrder(int userOrder) {
		this.userOrder = userOrder;
	}

	public String getBoundIp() {
		return boundIp;
	}

	public void setBoundIp(String boundIp) {
		this.boundIp = boundIp;
	}

	public Set<Group> getGroups() {
		return groups;
	}

	public void setGroups(Set<Group> groups) {
		this.groups = groups;
	}
	
	public void addRole(Role role){
		this.getRoles().add(role);
	}
	
	public void removeRole(Role role){
		if(this.getRoles().contains(role)){
			this.getRoles().remove(role);
		}
	}
	
	public void addGroup(Group group){
		this.getGroups().add(group);
	}
	
	public void removeGroup(Group group){
		if(this.getGroups().contains(group)){
			this.getGroups().remove(group);
		}
	}
	
	public String getSignPwd() {
		return signPwd;
	}

	public void setSignPwd(String signPwd) {
		this.signPwd = signPwd;
	}

	public String getSiteCodes() {
		return siteCodes;
	}

	public void setSiteCodes(String siteCodes) {
		this.siteCodes = siteCodes;
	}

	public String getLinkmen() {
		return linkmen;
	}

	public void setLinkmen(String linkmen) {
		this.linkmen = linkmen;
	}

	public String getZzdwCate() {
		return zzdwCate;
	}

	public void setZzdwCate(String zzdwCate) {
		this.zzdwCate = zzdwCate;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getPlainPassword() {
		return plainPassword;
	}

	public void setPlainPassword(String plainPassword) {
		this.plainPassword = plainPassword;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

}
